package com.letv.relatedstar;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.letv.conf.ConfigFactory;

public class StarLoading {
	private static final Logger logger = LoggerFactory.getLogger(StarLoading.class);
///leId to relateStar
	private static Map<Long, List<RelatedStar>> allStars = new HashMap<Long, List<RelatedStar>>();
	
	public static void initPath(String path) {
		
	}
	
	public static boolean load() {
		allStars.clear();
		BufferedReader br = null;
		try {
			String path  = ConfigFactory.getString("star.relateStarDataFile");
			if (path == null) {
				logger.error("star.relateStarDataFile config missed");
				return false;
			}
            br = new BufferedReader(new InputStreamReader(new FileInputStream(path),"UTF-8"));

            String str = null;
            
            while ((str = br.readLine()) != null) {
            	long starLeId = -1;
            	String[] str_spieces = str.split("\t");
            	if (str_spieces.length < 2) {
            		logger.error("invalid data line fromat");
//            		System.out.println("invalid data line fromat");
            		continue;
            	}
            	try {
            		starLeId = Long.parseLong(str_spieces[0]);
            		
            	} catch (Exception e) {
            		logger.error("invalid start le_id format:" + str_spieces[0]);
            		continue;
            	}
            	String relatedStarStr = str_spieces[1];
            	String[] starStrs = relatedStarStr.split(",");
            	List<RelatedStar> relatedList = new ArrayList<RelatedStar>();
            	for (int index = 0; index < starStrs.length; index++) {
            		String singleStarStr = starStrs[index];
            		if (singleStarStr.trim().equals("")) {
            			continue;
            		}
            		String[] relatedStarParts = singleStarStr.split(";");
            		if (relatedStarParts.length < 4){
            			logger.error("invalid single star format");
            			continue;	
            		}
            		long leId = -1;
            		String starName = "";
            		String starPic = "";
            		double relatedScore = 0.0;
            		try {
            			leId = Long.parseLong(relatedStarParts[0]);
            			starName = relatedStarParts[1];
            			starPic = relatedStarParts[2];
            			relatedScore = Double.parseDouble(relatedStarParts[3]);
            			
            		} catch (Exception e) {
            			logger.error("parse single star exception: " + e.toString());
            			continue;
            		}
            		RelatedStar star = new RelatedStar();
            		star.setLeId(leId);
            		star.setRelatedSore(relatedScore);
            		star.setStarName(starName);
            		star.setStarPic(starPic);
            		relatedList.add(star);	 
            	}
            	allStars.put(starLeId, relatedList);
            	
            }
            if (br != null) {
            	br.close();
            }
		} catch (Exception e) {
			logger.error("load exception:" + e.toString());
			return false;
			
		} 
		return true;
		
	}
	
	public static ArrayList<RelatedStar> findRelateList(long leId) {
		if (allStars.containsKey(leId))  {
			return (ArrayList<RelatedStar>) allStars.get(leId);
		}
		
		return null;
	}

}
